//
//  YTXYTXChartTools.h
//  Chart
//
//  Created by CaoJun on 15/2/15.
//
//

#ifndef __Chart__Tools__
#define __Chart__Tools__

#include <math.h>
#include <vector>
#include <limits>
#include <string>
#include <stdlib.h>
#include <sstream>
#include <iomanip>

namespace ytx
{
    class Tools
    {
        
    public:
        static double between(double min, double max, double num) {
            return fmax(min, fmin(max, num));
        };
        static std::string Convert2FixString(double value, int toFix = 2);
        
        static std::string numberToFixedPercentString(double valuePercent, int toFixed = 2);
        
        static std::string random();
        
        static double convert2Fix(double value, int toFix);
        
        static int parseInt(std::string s);
        
        static long parseLong(std::string s);
        
        static float parseFloat(std::string s);
        
        static double parseDouble(std::string s);
        
        static void swapFloat(float* f1, float* f2);
        
        static std::vector<double> slice(std::vector<double> data, int start, int count);
        
        static std::string url_encode(const std::string & str);
        
        static char to_hex(char code);
        
        static bool compareForMax(double a, double b);
        
        static bool compareForMin(double a, double b);
        
        static bool isNaN(double a);
        
        static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
        
        static std::vector<std::string> split(const std::string &s, char delim);
        
        static bool replace(std::string& str, const std::string& from, const std::string& to);
    };
    
    template <class T>
    std::string  ConvertToString(T value) {
        std::stringstream ss;
        ss << value;
        return ss.str();
    };
    
    template <typename T>
    bool isNumber(const T & a) {
        return a != std::numeric_limits<T>::quiet_NaN();
    }
    
    template <class T>
    int indexOf(const std::vector<T> & array, const T value){
        int index = -1;
        for(std::vector<double>::const_iterator it = array.begin();it != array.end();++it){
            if((*it) == value){
                index = (int)(it - array.begin());
            }
        }
        return index;
    }
}

#endif /* defined(__Chart__YTXTools__) */
